package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class Stock here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Stock
{
    private ArrayList<Product> stock;
    public static Stock instance;
    
    public Stock()
    {
        stock = new ArrayList<Product>();
    }

    
    public static Stock getSingelton()
    {
        if(instance == null)
        {
            instance = new Stock();
        }
        return instance;
    }
    
    public void createProduct(Product product)
    {
        stock.add(product);
    }
    
    public Product findProduct(int barcode)
    {
        int index = 0;
        boolean found = false;
        while(index < stock.size() && !found)
        {
            if(stock.get(index).getBarcode() == barcode)
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        return stock.get(index);
    }
    
    public void updateProduct(int barcode, int price, String name, int stocknumber, int min, int max, String supplier)
    {
        
        int index = 0;
        boolean found = false;
        String stringBarcode = Integer.toString(barcode);
        String stringPrice = Integer.toString(price);
        String stringStockNumber = Integer.toString(stocknumber);
        String stringMin = Integer.toString(min);
        String stringMax = Integer.toString(max);
        while(index < stock.size() && !found)
        {
            if(stock.get(index).getBarcode() == barcode)
            {
                if(stringBarcode.length() > 0)
                {
                    stock.get(index).setBarcode(barcode);
                }
                
                if(stringPrice.length() > 0)
                {
                    stock.get(index).setPrice(price);
                }
                
                if(name.length() > 0)
                {
                    stock.get(index).setName(name);
                }
                
                if(stringStockNumber.length() > 0)
                {
                    stock.get(index).setStocknumber(stocknumber);
                }
                
                if(stringMin.length() > 0)
                {
                    stock.get(index).setMin(min);
                }
                
                if(stringMax.length() > 0)
                {
                    stock.get(index).setMax(max);
                }
                
                if(supplier.length() > 0)
                {
                    stock.get(index).setSupplier(supplier);
                }
                found = true;
            }
            else
            {
                index++;
            }
        }
    }


    
    public void deleteProduct(int barcode)
    {
        int index = 0;
        boolean found = false;
        while(index < stock.size() && !found)
        {
            if(stock.get(index).getBarcode() == barcode)
            {
                stock.remove(index);
                found = true;
            }
            else
            {
                index++;
            }
        }
    }
    
    public ArrayList<Product> createOrderList()
    {
        int index = 0;
        ArrayList<Product> orderList = new ArrayList<Product>();
        while(index < stock.size())
        {
            Product product = stock.get(index);
            if(product.getMin() > product.getStocknumber())
            {
                orderList.add(product);
            }
            index++;
        }
        return orderList;
    }
}
